/**
 * @file     canConstruct.js
 * @brief    [383. 赎金信](https://leetcode.cn/problems/ransom-note/description/)
 * @author   Zhu
 * @date     2023-02-12 18:05
 */

/**
 * @param {string} ransomNote
 * @param {string} magazine
 * @return {boolean}
 */
var canConstruct = function (ransomNote, magazine) {
    const mDict = getDict(magazine);
    const rDict = getDict(ransomNote);

    for (const key in rDict) {
        if (!mDict[key] || mDict[key] < rDict[key]) return false;
    }

    return true;

    function getDict(str) {
        const map = {};
        for (const char of str) {
            map[char] = (map[char] ?? 0) + 1;
        }
        return map;
    }
};
